package com.github.aloxc.plugin.restplus.method;

import com.github.aloxc.plugin.DonotGuard;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求方法枚举
 * @author leero
 */
@DonotGuard
public enum HttpMethod {
    /**
     * GET
     */
    @DonotGuard
    GET,

    /**
     * POST
     */
    @DonotGuard
    POST,

    /**
     * PUT
     */
    @DonotGuard
    PUT,

    /**
     * DELETE
     */
    @DonotGuard
    DELETE,

    /**
     * PATCH
     */
    @DonotGuard
    PATCH,

    /**
     * HEAD
     */
    @DonotGuard
    HEAD,

    /**
     * OPTIONS
     */
    @DonotGuard
    OPTIONS,

    /**
     * TRACE
     */
    @DonotGuard
    TRACE,

    /**
     * CONNECT
     */
    @DonotGuard
    CONNECT,

    /**
     * PURGE
     */
    @DonotGuard
    PURGE,
    /**
     * post带body
     */
    @DonotGuard
    POST_BODY;
    private static final Map<String, HttpMethod> METHOD_MAP = new HashMap(8);


    public static HttpMethod getByRequestMethod(String method) {
        if (method == null || method.isEmpty()) {
            return null;
        }

        String[] split = method.split("\\.");

        if (split.length > 1) {
            method = split[split.length - 1].toUpperCase();
            return HttpMethod.valueOf(method);
        }

        return HttpMethod.valueOf(method.toUpperCase());
    }

    static {
        for (HttpMethod httpMethod : values()) {
            METHOD_MAP.put(httpMethod.name(), httpMethod);
        }
    }

}

